<?php
/**
 *  矿机视频
*/

namespace app\api\service;


use app\api\model\MinerVideos as ModelMinerVideos;
use app\facade\Basic;
use think\facade\Db;

class MinerVideo
{
    /**
     *  矿机视频列表
    */
    public function videoList($param)
    {

        $where[] = ['is_del','=',0];
        $where[] = ['showstate','=',1];
        #处理排序
        $order = 'db_miner_videos.create_time desc';
        if(isset($param['sort']) && $param['sort'] == 2){
            $order = 'db_miner_videos.sort asc';
        }

        if (isset($param['category']) && !empty($param['category'])) {

            $where[] = ['category', '=',  $param['category']];
        }


        /* 查询 */
        $result = ModelMinerVideos::with([
            'model_name' => function ($query) {
            return $query->field('id,model_name');
        },'coverimage' => function ($query){
            return $query->field('id,url,name');
        },'video' => function ($query) {
            return $query->field('id,url,name');
        }])
            ->where($where);

        if(isset($param['id']) && !empty($param['id'])) {
            $result = $result->whereRaw("FIND_IN_SET(?, pm_ids)", [$param['id']]);
        }
        $total = $result->count();
        if(!$total){
            return Basic::data(0, 'ok', ['list'=>[],'total' =>0,'product'=>[]]);
        }
        $result= $result->field('id,model_id,video_title,youtube_vid,pm_ids,blogger_name,publish_time,category,coverimage_id,video_id,init_shownumber,shownumber,create_time')
            ->order($order)
            ->page($param['page'], $param['pageSize'])->select();
        #取聚合商品信息
        $product = [];
        $ids = [];

       $data = $result->toArray();
        foreach ($data as $key =>$value ) {
            $data[$key]['coverimage_url'] = '';
            if(isset($value['model_name'])) {
               $data[$key]['model_name'] = $value['model_name']['model_name'];
            } else {
               $data[$key]['model_name'] = '';
            }
            if ( !empty($value['youtube_vid']) ) {
                /* youtube 视频 */
               $data[$key]['video_url'] = 'https://www.youtube.com/watch?v='.$value['youtube_vid'];
               $data[$key]['coverimage_url'] = "https://cdn.minetheasic.com/vi/{$value['youtube_vid']}/maxresdefault.jpg?x-oss-process=image/resize,m_lfit,w_500,limit_0/quality,q_100";
            } elseif ( !empty($value['video_id'])) {


                /* 上传的视频地址 */
                if(!empty($value['video'])) {

                   $data[$key]['video_url'] = $value['video']['url'];
                } else {
                   $data[$key]['video_url'] = '';
                }
            } else {
               $data[$key]['video_url'] = '';
               $data[$key]['coverimage_url'] = '';
            }
            /* 上传的封面 */

            if(!empty($value['coverimage'])) {
                $data[$key]['coverimage_url'] = $value['coverimage']['url'];
            }

            unset($data[$key]['coverimage']);
            unset($data[$key]['video']);
        }
        #存在聚合商品 id   且是第三方视频 场景  需返回商品信息
        if($param['category'] == 1){
            $list = ModelMinerVideos::where([
                    'is_del' => 0,
                    'showstate' => 1,
                    'category' => 1,
            ])->field('pm_ids')->select()->toArray();
            #三方视频 关联的 聚合商品 展示
            if(!empty($list)){
                foreach ($list as $k=>$v){
                    if(!empty($v['pm_ids'])){
                        $ids = array_merge($ids,explode(',',$v['pm_ids']));
                    }
                }
            }
            if(!empty($ids)){
                $ids = array_unique($ids);
                $product = Db::name('product_merge')->where('id','in',$ids)->field('id,productname')->select()->toArray();
            }

        }

        return Basic::data(0, 'ok', ['list'=>$data,'total' =>$total,'product'=>$product]);
    }

    /**
     *  矿机视频详情
    */
    public function detail($param)
    {

        $where[] = ['id','=',$param['id']];
        $where[] = ['is_del','=',0];
        $where[] = ['showstate','=',1];


        ModelMinerVideos::where($where)->setInc('shownumber');


        $result = ModelMinerVideos::with(['model_name'=>function($query){
        },'coverimage'=>function($query){
            return $query->field('id,url,name');
        },'video' => function ($query) {
            return $query->field('id,url,name');
        }])
            ->where($where)
            ->field('id,model_id,video_title,youtube_vid,coverimage_id,video_id,init_shownumber,shownumber,create_time')
            ->find();

        if ( !$result ) {
            return Basic::data(1, '未找到');
        }

       $data['data'] = $result->toArray();


        $basic_url = 'https://' . config('filesystem.aliyun-oss.bucket') . '.' . config('filesystem.aliyun-oss.endpoint') . '/';

        /* 负责人 */
        if(isset($data['model_name'])) {
           $data['data']['model_name'] =$data['data']['model_name']['model_name'];
        } else {
           $data['data']['model_name'] = '';
        }


       $data['data']['video_url'] = '';
       $data['data']['coverimage_url'] = '';

        if ( !empty($data['youtube_vid']) ) {
            /* youtube 视频 */
           $data['data']['video_url'] = 'https://www.youtube.com/watch?v='.$data['youtube_vid'];
           $data['data']['coverimage_url'] = "https://cdn.minetheasic.com/vi/{$data['youtube_vid']}/maxresdefault.jpg?x-oss-process=image/resize,m_lfit,w_500,limit_0/quality,q_100";
        } else {

            /* 上传的视频封面 */
            if(isset($data['coverimage'])) {
               $data['data']['coverimage_url'] = $basic_url.$data['coverimage']['url'];
            }
            /* 上传的视频地址 */
            if(isset($data['video'])) {
               $data['data']['video_url'] = $basic_url.$data['video']['url'];
            }

        }

        unset($data['coverimage']);
        unset($data['video']);
        unset($data['coverimage_id']);
        unset($data['youtube_vid']);
        unset($data['video_id']);


        if ( $result ) {
            return Basic::data(0, 'ok',$data['data']);
        } else {
            return Basic::data(1, '获取失败');
        }
    }

}
